layui.use(['form', 'table','layuimini','element', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        tableSelect = layui.tableSelect;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoPhoto = {
        id: 'BoPhotoTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'photoAlbumName', title: '相册名称', align: 'center'},
            {field: 'thumUrl', title: '照片', align: 'center',
                templet: function (object) {
                    var img = '';
                    if(object.type == '0'){
                        img = '<img src="/photo_img/' + object.thumUrl +
                            '" lay-event="previewImg"' + ' style="cursor: pointer;max-width:100px; max-height:100px;"';
                    }else{
                        img = '<video lay-event="previewImg" style="cursor: pointer;max-width:100px; max-height:100px;"><source src="/photo_img/'+ object.url +'" type="video/mp4">您的浏览器不支持视频。</video>';
                    }
                    return img;
                }
            },
            {field: 'remark', title: '备注', align: 'center',
                templet: function (object) {
                    if(object.remark == '' || object.remark == null){
                        return '-';
                    }else{
                        return object.remark;
                    }
                }
            },
            {field: 'type', title: '类型', align: 'center',
                templet: function (object) {
                    if(object.type == 0){
                        return '<span class="layui-badge layui-bg-green">照片</span>';
                    }else{
                        return '<span class="layui-badge layui-bg-cyan">视频</span>';
                    }
                }
            },
            {field: 'imgSize', title: '图片大小', align: 'center',
                templet: function (object) {
                    var size = object.imgSize / 1024 / 1024;
                    return size.toFixed(3) + 'M';
                }
            },
            {field: 'thumSize', title: '缩略图大小', align: 'center',
                templet: function (object) {
                    var size = object.thumSize / 1024 / 1024;
                    return size.toFixed(3) + 'M';
                }
            },
            {field: 'updateDate', title: '更新时间', minWidth: 120, align: 'center'},
            {field: 'createDate', title: '上传时间', minWidth: 120, align: 'center'},
            {templet: '#stateBtn', width: 140, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoPhoto.id,
        url: '/web/boPhoto/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var photoAlbumId = $("#search_photoAlbumId").val();
        var type = $("#search_type").val();

        //封装
        var dataObject = {};
        dataObject['photoAlbumId'] = photoAlbumId;
        dataObject['type'] = type;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhoto.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_photoAlbumId").val("");
        $("#search_photoAlbumName").val("");
        $("#search_type").val("-1");

        form.render();

        //封装
        var dataObject = {};
        dataObject['photoAlbumId'] = "";
        dataObject['type'] = "-1";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoPhoto.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    //下拉选择
    //默认值:只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
    tableSelect.render({
        elem: '#search_photoAlbumName',	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'name',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '相册名称搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/web/boPhotoAlbum/list',
            cols: [
                [
                    {type: 'radio'},
                    {field: 'name', title: '相册名称', align: 'center'}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#search_photoAlbumName").val(data.data[0].name);
            $("#search_photoAlbumId").val(data.data[0].id);
        }
    });

    // 监听上传照片操作
    $(".layuimini-container").on("click", "#add-btn", function () {
        var content = layuimini.getHrefContent('/web/boPhoto/add');
        var index = layer.open({
            title: '上传照片',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['700px','650px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPhoto.id);
            }
        });

        return true;
    });

    // 监听上传视频操作
    $(".layuimini-container").on("click", "#add-video-btn", function () {
        var content = layuimini.getHrefContent('/web/boPhoto/addVideo');
        var index = layer.open({
            title: '上传视频',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['700px','650px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPhoto.id);
            }
        });

        return true;
    });

    /**
     * 点击编辑按钮
     * @param id
     */
    BoPhoto.edit = function(id){
        var content = layuimini.getHrefContent('/web/boPhoto/edit?id=' + id);
        var index = layer.open({
            title: '编辑照片',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['700px','650px'], //宽高
            content: content,
            end: function(){
                //刷新表格
                table.reload(BoPhoto.id);
            }
        });

        return false;
    };

    /**
     * 点击删除按钮
     * @param id
     */
    BoPhoto.delete = function(id){
        layer.open({
            title: '删除照片'
            ,content: '是否确定删除该照片？'
            ,icon: 3
            ,btn: ['确定', '取消']
            ,yes: function(index, layero){
                layer.close(index);
                var indexLoad = layer.load(2);
                var url = '/web/boPhoto/delete';
                $.post(url, {id: id}, function(data){
                    layer.close(indexLoad);
                    if(data.state == "200"){
                        layer.msg(data.message, {icon: 1});
                        table.reload(BoPhoto.id);
                    }else{
                        layer.msg(data.message, {icon: 2});
                    }
                })
            }
        });

        return false;
    };

    //照片预览
    BoPhoto.previewImg = function(data) {

        var width = (window.screen.width - 400) + 'px';
        var height = (window.screen.height - 300) + 'px';

        var imgHtml = '';

        if(data.type == '0'){
            imgHtml = "<img src='/photo_img/" + data.thumUrl + "' style='min-width:100px; min-height:100px;max-width:" + width + ";max-height:" + height + ";'/>";
        }else{
            imgHtml = '<video controls autoplay style="cursor: pointer;width:600px; height:800px;"><source src="/photo_img/'+ data.url +'" type="video/mp4">您的浏览器不支持视频。</video>'
        }

        //捕获页
        var index = layer.open({
            type: 1,
            title: false, //不显示标题
            shadeClose: true,
            skin: 'previewImg-class', //透明
            area: ['auto', 'auto'],
            content: imgHtml //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        });
        $(window).on("resize", function () {
            layer.full(index);
        });
    };

    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoPhoto.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            BoPhoto.delete(data.id);
        } else if (layEvent === 'edit') {
            BoPhoto.edit(data.id)
        } else if(layEvent === 'previewImg'){
            BoPhoto.previewImg(data);
        }
    });

});